package io.wongxd.demo.page

import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import io.wongxd.demo.AppBtn
import io.wongxd.demo.V
import io.wongxd.solution.baseView.BaseComposeToolBarFgt
import io.wongxd.solution.compose.composeTheme.AppThemeHolder
import io.wongxd.solution.compose.composeTheme.appColor
import io.wongxd.solution.compose.composeTheme.textStyle
import io.wongxd.solution.compose.helper.LocationCompose
import io.wongxd.solution.permission.PermissionCommons
import io.wongxd.solution.permission.PermissionCommons.isPermissionGranted

class FgtLocation : BaseComposeToolBarFgt() {
    override fun lazyInit() {

    }

    override val title: String
        get() = "FgtLocation"

    @Composable
    override fun Render() {

        val str = remember {
            mutableStateOf("")
        }

        val reqRefreshLocationMs = remember {
            mutableStateOf(0L)
        }

        val locationPer = remember {
            mutableStateOf(requireActivity().isPermissionGranted(*PermissionCommons.REQUIRED_LOCATION_PERMISSIONS))
        }

        PermissionCommons.ReqPermission(
            permissionsToRequest = PermissionCommons.REQUIRED_LOCATION_PERMISSIONS,
            permissionResult = {
                locationPer.value = it
            })

        if (locationPer.value)
            LocationCompose(
                pauseAfterSuccess = true,
                refreshReqMs = reqRefreshLocationMs.value,
                onChange = { location, lat, lon, address ->
                    str.value = location.toString() + "\n" + address?.toString()
                })

        Column(Modifier, horizontalAlignment = Alignment.CenterHorizontally) {
            Text(text = str.value, style = AppThemeHolder.textStyle.sp16.appColor())

            V(number = 20)

            AppBtn("重新定位") {
                reqRefreshLocationMs.value = System.currentTimeMillis()
            }
        }
    }

    @Preview(showBackground = true, backgroundColor = 0xffffffff)
    @Composable
    fun RenderPreView() {

    }
}